package com.ziyueyuwen.xiniao.controller.student.lesson;

import com.alibaba.fastjson.JSONObject;
import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.homework.ZyHomeworkRecordDO;
import com.ziyueyuwen.xiniao.bean.vo.ZyClassVO;
import com.ziyueyuwen.xiniao.bean.vo.ZyLessonVO;
import com.ziyueyuwen.xiniao.dao.ZyLessonMessageTaskMapper;
import com.ziyueyuwen.xiniao.enums.MessageTypeEnum;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.service.ClassService;
import com.ziyueyuwen.xiniao.service.HomeworkRecordService;
import com.ziyueyuwen.xiniao.service.LessonService;
import com.ziyueyuwen.xiniao.service.SubjectQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author wancheng  on 2018/12/22.
 */
@Controller
@RequestMapping("/student/lesson")
public class StudentLessonController {

    @Autowired
    LessonService lessonService;
    @Autowired
    ZyLessonMessageTaskMapper zyLessonMessageTaskMapper;
    @Autowired
    HomeworkRecordService homeworkRecordService;
    @Autowired
    SubjectQuestionService subjectQuestionService;

    @RequestMapping(value = "/get_lesson_by_class_id.do",method = RequestMethod.POST)
    public void getLessonByClassId(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestParam(PARAM_CLASS_ID)Integer classId) {
        HttpResContent<Map<String,Object>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取学生的课课节列表");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            Map<String,Object> res = lessonService.getStudentLessonByClassId(classId,userAccount);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_task.do",method = RequestMethod.POST)
    public void getLessonTask(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(PARAM_LESSON_ID)Integer lessonId) {
        HttpResContent<ZyLessonVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课节发送消息任务列表");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_LESSON_ID, lessonId);
            String userAccount = hrc.getUserInfo().getUserAccount();
            ZyLessonVO res = lessonService.getStudentLessonTaskByLessonId(lessonId,userAccount);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/confirm_lesson_task.do",method = RequestMethod.POST)
    public void confirmTask(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(PARAM_LESSON_ID)Integer lessonId,
                            @RequestParam(PARAM_TYPE)String type) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生报告数据收集:");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            MessageTypeEnum messageTypeEnum = MessageTypeEnum.valueOf(type);
            lessonService.confirmLessonTask(lessonId,userAccount,messageTypeEnum);
            lessonService.studentLessonReportDataCollect(lessonId,userAccount,messageTypeEnum);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_recently.do", method = RequestMethod.POST)
    public void getHomeworkRecently() {
        HttpResContent<ZyLessonVO> hrc = new HttpResContent<>("获取最近课节");
        String userAccount = hrc.getUserInfo().getUserAccount();
        Integer lessonId = zyLessonMessageTaskMapper.getLessonHomeworkRecently(userAccount);
        ZyLessonVO lessonVO = lessonService.getClassAndCourseByLessonId(lessonId);
        if (lessonVO == null){
            hrc.printLogAndResponse(null);
        }
        ZyHomeworkRecordDO recordDO = homeworkRecordService.getByStudentIdAndLessonId(userAccount,lessonId);
        if (recordDO != null){
            lessonVO.setIfFinishedHomework(recordDO.getIfFinishAnswer());
        }else{
            lessonVO.setIfFinishedHomework(false);
        }
        JSONObject answerOrder = null;
        try {
            answerOrder = subjectQuestionService.getAnswerOrder(lessonVO.getCourse().getId(),lessonVO.getLessonIndex());
        } catch (Exception e) {
            e.printStackTrace();
        }
        lessonVO.setAnswerOrder(answerOrder);
        hrc.printLogAndResponse(lessonVO);
    }
}
